package org.beetl.json.node;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.beetl.json.JsonWriter;
import org.beetl.json.Location;
import org.beetl.json.LocationAction;
import org.beetl.json.OutputNode;
import org.beetl.json.action.IValueAction;
import org.beetl.json.loc.IndexLocation;
import org.beetl.json.loc.IndexLocations;

public class IterableClassNode extends  OutputNode {
	
	@Override
	public void render(Object obj, JsonWriter w) throws IOException {
		
		Object attrValue = obj;
		if(valueActions.size()==0){
			if(obj instanceof Iterator){
				NodeUtil.writeIterator (this, (Iterator)obj, w);
			}else{
				NodeUtil.writeIterator (this, ((Iterable)obj).iterator(), w);
			}
			
			
		}else{
			NodeUtil.writeUnkonw(this, obj, w, valueActions);
		}
	}

	@Override
	public void setActionByLocation(List<Location> list) {
		for(Location l:list){			
			LocationAction action = l.getAction();
			if(l instanceof IndexLocation){
				if(this.indexLocation==null){
					this.indexLocation = new IndexLocations();
				}
				IndexLocation il = (IndexLocation)l;				
				this.indexLocation.addIndexLocation(il);
				
				
			}else{
				 if(action instanceof IValueAction){
					if(isMatch(l)){						 
						 this.valueActions.add( (IValueAction)action);
					 }					
				}
			}
				
		}
	}
	
	protected boolean isMatch(Location location) {
		return location.match(this,Iterable.class,null);
	}

	

	
	
	
	

}
